package com.ljf.tijian.service;

import com.ljf.tijian.ApiResponse;
import com.ljf.tijian.dto.OrdersDto;
import com.ljf.tijian.exception.TijianAppException;
import com.ljf.tijian.exception.TijianExceptionEnum;
import com.ljf.tijian.mapper.OrdersMapper;
import com.ljf.tijian.mapper.SetmealMapper;
import com.ljf.tijian.pojo.Orders;
import com.ljf.tijian.pojo.Setmeal;
import com.ljf.tijian.service.Impl.OrdersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class OrdersService implements OrdersServiceImpl {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public ApiResponse createOrder(Orders orders) {
        //判断当前用户在预约当天有没有其他订单
        Orders orderResult = ordersMapper.selectByUserIdAndOrderDate(orders);
        if(orderResult != null){

//            throw new TijianAppException(TijianExceptionEnum.Already_Appointment.getCode(),TijianExceptionEnum.Already_Appointment.getMessage());
            return new ApiResponse(TijianExceptionEnum.Already_Appointment.getCode(), TijianExceptionEnum.Already_Appointment.getMessage());
        }

        ordersMapper.insert(orders);
        return new ApiResponse(200,"SUCCESS",orders);
    }

    @Override
    public ApiResponse selectAllOrdersForSmId() {

        //获取所有的订单
        List<OrdersDto> orders = ordersMapper.selectAllOrdersForSmId();

        //遍历订单进行订单统计
        Map<String,Long> ordersMap = new HashMap<>();
        for (OrdersDto order : orders) {
            ordersMap.merge(order.getName(),1L,Long::sum);
        }

        List<Map<String, Object>> resultmap = new ArrayList<>();
        for (String s : ordersMap.keySet()) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",s);
            map.put("value",ordersMap.get(s));
            resultmap.add(map);
        }

        return new ApiResponse(200,"SUCCESS",resultmap);
    }

    @Override
    public ApiResponse selectAllOrdersForHpId() {
        //获取所有的订单
        List<OrdersDto> orders = ordersMapper.selectAllOrdersForHpId();

        //遍历订单进行订单统计
        Map<String,Long> ordersMap = new HashMap<>();
        for (OrdersDto order : orders) {
            ordersMap.merge(order.getName(),1L,Long::sum);
        }

        List<Map<String, Object>> resultmap = new ArrayList<>();
        for (String s : ordersMap.keySet()) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",s);
            map.put("value",ordersMap.get(s));
            resultmap.add(map);
        }

        return new ApiResponse(200,"SUCCESS",resultmap);
    }

    @Override
    public ApiResponse queryAppointmentByUser(Orders orders) {

        List<Orders> ordersList = ordersMapper.selectByUserIdAndStateList(orders);
        for (Orders order : ordersList) {
            Setmeal setmeal =  setmealMapper.selectByPrimaryKey(order.getSmId());
            order.setSetmeal(setmeal);
            Long currentTime = new Date().getTime();
            Long orderTime = order.getOrderDate().getTime();
            Float differentTime = (orderTime-currentTime)/(1000*60*60*24)*1F;
            if(differentTime<3){
                order.setIsCancle(0);
            }else{
                order.setIsCancle(1);
            }
        }


        return new ApiResponse(200,"SUCCESS",ordersList);
    }

    @Transactional
    @Override
    public ApiResponse cancelOrder(Orders orders) {
        //1.先把订单查询出来
        Orders order = ordersMapper.selectByPrimaryKey(orders.getOrderId());
        //2.判定订单是否超过取消期限
        Long currentTime = new Date().getTime();
        Long orderTime = order.getOrderDate().getTime();
        Float differentTime = (orderTime-currentTime)/(1000*60*60*24)*1F;
        if(differentTime <= 3){
            return new ApiResponse(TijianExceptionEnum.OVER_DADALINE.getCode(), TijianExceptionEnum.OVER_DADALINE.getMessage());
        }else{
            //3.取消订单
            order.setState(4);
            ordersMapper.updateByPrimaryKeySelective(order);
            return new ApiResponse(200,"SUCCESS");
        }

    }

    @Override
    public ApiResponse updateOrder(Orders orders) {

        Orders order = ordersMapper.selectByPrimaryKey(orders.getOrderId());

        if(order == null){
            return new ApiResponse(TijianExceptionEnum.Appointment_NOT_EXIST.getCode(), TijianExceptionEnum.Appointment_NOT_EXIST.getMessage());
        }
        return null;
    }


}
